package cn.tedu.jdbc;

import java.sql.*;
import java.util.Scanner;

public class JDBCReg {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()) {
            Statement statement = connection.createStatement();
            Scanner scanner = new Scanner(System.in);
            String username = scanner.nextLine();// 用户名
            String password = scanner.nextLine();// 密码
            String nickname = scanner.nextLine();// 昵称
            Integer age = scanner.nextInt();// 年龄
            String sql = "select * from userinfo where username = ?";
            PreparedStatement ps =  connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet r = ps.executeQuery();
            if (r.next()){
                System.out.println("用户名已存在");
             }else {
                sql = "insert into userinfo(username,password,nickname,age) values('" + username + "','" + password + "','" + nickname + "'," + age + ")";
                int i = statement.executeUpdate(sql);
                System.out.println("注册成功");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
